package com.lyw.h2andjava.controller;

import com.lyw.h2andjava.entity.User;
import com.lyw.h2andjava.service.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Component
public class AutoSelectTable implements ApplicationRunner {

    @Autowired
    private UserRepository userRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        while (true){
            try {
                Thread.sleep(10000);
                List<User> users = userRepository.findAll();
                System.out.println(users.toString());
//                Robot r   =   new   Robot();
//                r.delay(   2000   );
                //java里面的sleep()并不能精确定时，TimeTask可以：例下面的小程序：
//                Timer timer = new Timer();// 实例化Timer类
//                timer.schedule(new TimerTask() {
//                    public void run() {
//                        List<User> users = userRepository.findAll();
//                        System.out.println(users.toString());
//                        this.cancel();
//                    }
//                }, 5000);// 这里百毫秒

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
